﻿using MVCMamage.DAL;
using MVCManage.Model;
using MVCManage.Model.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVCManage.BLL
{
    public class MenusBLL : BaseBLL<Menus>
    {
        public override void SetDal()
        {
            Dal = new MenusDAL();
        }
        AuthorityDAL dd = new AuthorityDAL();
        public List<Menus> GetMenus(string name,int page,int size,out int count) {
            var zhi = Dal.Query();
            if (!string.IsNullOrWhiteSpace(name)) {
                zhi = zhi.Where(n => n.menuName.Contains(name));
            }
            count = zhi.Count();
            var pagelist = zhi.OrderBy(n => n.menuID).Skip((page - 1) * size).Take(size);
            return pagelist.ToList();
        }
        public bool Delete(int id) {
            var zhi = Dal.Query().Where(n => n.menuID == id).FirstOrDefault();
            var hh = dd.Query().Where(n => n.menuID == id).FirstOrDefault();
            if (hh != null)
            {
                dd.Delete(hh);
                Dal.Delete(zhi);
            }
            else {
                Dal.Delete(zhi);
            }
            return Dal.SaveChanges();

        }
        public bool Deletes(int[] ids) {
            var zhi = Dal.Query().Where(n => ids.Contains(n.menuID)).ToList();
            var hh = dd.Query().Where(n => ids.Contains(n.menuID)).ToList();
            if (hh != null)
            {
                dd.Deletes(hh);
                Dal.Deletes(zhi);
            }
            else {
                Dal.Deletes(zhi);
            }
                
            return Dal.SaveChanges();
        }


    }
}
